home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Telnet 2.7b5 / source / Screens / vsinterf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-12  |  43.2 KB  |  375 lines  |  [TEXT/CWIE]

  1. /*
  2.  *
  3.  *      Virtual Screen Kernel Interface
  4.  *              (vsinterf.c)
  5.  *
  6.  *      by Gaige B. Paulsen
  7.  *
  8.  *    This file contains the control and interface calls for the NCSA
  9.  *  Virtual Screen Kernal.
  10.  *
  11.  *      VSinit(maxwidth)                    - Initialize the VSK
  12.  *      VSnewscreen(maxlines,scrnsave)    - Initialize a new screen.
  13.  *      VSdetach(w)                        - Detach screen w
  14.  *      VSredraw(w,x1,y1,x2,y2)            - redraw region for window w
  15.  *      VSwrite(w,ptr,len)                - write text @ptr, length len
  16.  *      VSclear(w)                        - clear w's real screen
  17.  *      VSkbsend(w,k,echo)                - send keycode k's rep. out window w (w/echo if req.)
  18.  *      VSclearall(w)                        - clear w's real and saved screen
  19.  *      VSreset(w)                        - reset w's emulator (as per TERM)
  20.  *      VSgetline(w,y)                    - get a ptr to w's line y
  21.  *      VSsetrgn(w,x1,y1,x2,y2)            - set local display region
  22.  *      VSscrolback(w,n)                    - scrolls window w back n lines
  23.  *      VSscrolforward(w,n)                - scrolls window w forward n lines
  24.  *      VSscrolleft(w,n)                     - scrolls window w left  n columns
  25.  *      VSscrolright(w,n)                     - scrolls window w right n columns
  26.  *      VSscrolcontrol(w,scrlon,offtop)    - sets scroll vars for w
  27.  *      VSgetrgn(w,&x1,&y1,&x2,&y2)        - returns set region
  28.  *      VSsnapshot(w)                          - takes a snapshot of w
  29.  *      VSgetlines(w)                        - Returns current # of lines
  30.  *      VSsetlines(w, lines)                - Sets the current # of lines to lines
  31.  *    
  32.  *        Version Date    Notes
  33.  *        ------- ------  ---------------------------------------------------
  34.  *        0.01    861102  Initial coding -GBP
  35.  *        0.10    861113  Added some actual program to this file -GBP
  36.  *        0.15    861114  Initiated Kludge Operation-GBP
  37.  *        0.50    8611VSPBOTTOM  Parameters added to VSnewscreen -GBP
  38.  *        0.90    870203    Added the kbsend routine -GBP
  39.  *        2.1        871130    NCSA Telnet 2.1 -GBP
  40.  *        2.2     880715    NCSA Telnet 2.2 -GBP
  41.  *
  42.  */
  43.  
  44. #ifdef MPW
  45. #pragma segment VS
  46. #endif
  47.  
  48. #define VSMASTER
  49.  
  50.  
  51. #include "rsinterf.proto.h"
  52. #include "rsmac.proto.h"
  53. #include "vsem.proto.h"
  54. #include "vsdata.h"
  55. #include "vskeys.h"
  56. #include "vsinit.h"
  57. #include "vsintern.proto.h"
  58. #include "Wind.h"
  59. #include "DlogUtils.proto.h"
  60. #include "maclook.proto.h"
  61. #include "errors.proto.h"
  62. #define DEBUGMAC
  63.  
  64. #include "vsinterf.proto.h"
  65.  
  66. extern TelInfoRec *TelInfo;
  67. extern WindRec    *screens;
  68. extern long        TempItemsDirID;
  69. extern short    TempItemsVRefNum;
  70.  
  71. short
  72.   /* Internal variables for use in managing windows */
  73.     VSmax = 0, /* max nr screens allowed */
  74.     VSinuse = 0; /* nr screens actually in existence */
  75. VSscrndata *VSscreens;
  76.  
  77. short VSinit
  78.   (
  79.     short max /* max nr screens to allow */
  80.   )
  81.   /* initializes virtual screen and window handling. */
  82.   {
  83.     short i;
  84.  
  85.     RSinitall(max);
  86.     VSmax = max;
  87.     VSIwn = 0;
  88.     if ((VSscreens = (VSscrndata *) myNewPtr(max * sizeof(VSscrndata))) == 0L)
  89.         return(-2);
  90.     for (i = 0; i < max; i++)
  91.       {
  92.         VSscreens[i].loc = 0L;
  93.         VSscreens[i].stat = 0;
  94.       } /* for */
  95.     return(0);
  96.   } /* VSinit */
  97.  
  98.  
  99. short VSiscapturing(short w) {                                /* BYU 2.4.18 */
  100.     return(VSscreens[w].captureRN);                    /* BYU 2.4.18 */
  101. }                                                        /* BYU 2.4.18 */
  102.  
  103. Boolean VSopencapture(short scrn_num, short w)
  104. {
  105.     UNUSED_ARG(scrn_num)
  106.     static short    captNumber = 1;
  107.     short            VRefNum;
  108.     long            DirID;
  109.     Str255            filename, tempString;
  110.     Str32            numstring;
  111.     Point            where = {100,100};
  112.     SFReply            sfr;
  113.     long            junk = 0;
  114.     OSErr            err;
  115.     
  116.     NumToString(captNumber++, numstring);
  117.     GetIndString(filename, MISC_STRINGS, CAPTFILENAME);
  118.     filename[++(filename[0])] = ' ';
  119.     pstrcat(filename, numstring);
  120.     
  121.     GetIndString(tempString,MISC_STRINGS,SAVE_CAPTURED_TEXT_STRING);
  122.     SFPutFile(where,tempString, filename, NULL, &sfr);
  123.     if (sfr.good) {
  124.         (void) GetWDInfo(sfr.vRefNum, &VRefNum, &DirID, &junk);
  125.  
  126.         err = HCreate(VRefNum, DirID, sfr.fName, 
  127.                     gApplicationPrefs->CaptureFileCreator, 'TEXT');
  128.         if (err == dupFNErr) {
  129.             HDelete(VRefNum, DirID, sfr.fName);
  130.             err = HCreate(VRefNum, DirID, sfr.fName,
  131.                             gApplicationPrefs->CaptureFileCreator, 'TEXT');
  132.             }
  133.             
  134.         if (err != noErr)
  135.             OperationFailedAlert(CANT_CREATE_FILE, 500, err);
  136.         else {        
  137.             err = HOpen(VRefNum, DirID, sfr.fName, fsRdWrPerm,
  138.                 &VSscreens[w].captureRN);
  139.             if (err != noErr) OperationFailedAlert(CANT_OPEN_FILE, 501, err);
  140.             else {
  141.                 SetEOF(VSscreens[w].captureRN, (long) 0);
  142.                 return(TRUE);
  143.                 }
  144.             }
  145.         }
  146.         
  147.     return(FALSE);
  148. }
  149.  
  150. void VSclosecapture(short w) {                                    /* BYU 2.4.18 */
  151.     FSClose(VSscreens[w].captureRN);                    /* BYU 2.4.18 */
  152.     VSscreens[w].captureRN = 0;                            /* BYU 2.4.18 */
  153. }                                                        /* BYU 2.4.18 */
  154.  
  155. void VScapture(unsigned char *ptr, short len) {        /* BYU 2.4.18 */
  156.     long ln = len;                                /* BYU 2.4.18 */
  157.     if (VSscreens[VSIwn].captureRN) {            /* BYU 2.4.18 */
  158.         unsigned char captbuf[512];                /* BYU 2.4.18 */
  159.         unsigned char *ptr2,*ptr3;                /* BYU 2.4.18 */
  160.         ptr2 = ptr;                                /* BYU 2.4.18 */
  161.         ptr3 = &captbuf[0];                        /* BYU 2.4.18 */
  162.         for (len = 0; len < ln; len++) {        /* BYU 2.4.18 */
  163.             if (*ptr2 >= 32 ||                     /* BYU 2.4.18 */
  164.                 *ptr2 == 13 ||                    /* BYU 2.4.18 */
  165.                 *ptr2 ==  9)                    /* BYU 2.4.18 */
  166.                 *(ptr3++) = *(ptr2++);            /* BYU 2.4.18 */
  167.             else {                                /* BYU 2.4.18 */
  168.                 ptr2++;                            /* BYU 2.4.18 */
  169.                 ln--;                            /* BYU 2.4.18 */
  170.             }                                    /* BYU 2.4.18 */
  171.         }                                        /* BYU 2.4.18 */
  172.         if (ln > 0) {                                                    /* BYU 2.4.18 */
  173.             if (FSWrite(VSscreens[VSIwn].captureRN, &ln, captbuf)) {    /* BYU 2.4.18 */
  174.                 FSClose(VSscreens[VSIwn].captureRN);                    /* BYU 2.4.18 */
  175.                 VSscreens[VSIwn].captureRN = 0;                            /* BYU 2.4.18 */
  176.             }                                            /* BYU 2.4.18 */
  177.         }                                            /* BYU 2.4.18 */
  178.     }                                                /* BYU 2.4.18 */
  179. }                                                    /* BYU 2.4.18 */
  180.  
  181. short    VSisprinting(short w)
  182. {
  183.     return((VSscreens[w].loc)->prredirect);
  184. }
  185.  
  186. void    ClosePrintingFile(short w)
  187. {
  188.     OSErr sts;
  189.     char tmp[80];
  190.  
  191.     putln("Attempting to remove print file");
  192.     
  193.     if ((sts=FSClose ((VSscreens[w].loc)->refNum)) != noErr) {
  194.         SysBeep(1);
  195.         sprintf(tmp,"FSClose: ERROR %d",sts); putln(tmp);
  196.         }
  197.     if ((sts=HDelete(TempItemsVRefNum, TempItemsDirID, (StringPtr)VSIw->fname)) != noErr) {
  198.         SysBeep(1);
  199.         sprintf(tmp,"HDelete: ERROR %d",sts); putln(tmp);
  200.         }
  201. }
  202.  
  203. short VSvalids
  204.   (
  205.     short w
  206.   )
  207.   /* validates a virtual screen number and sets it as the
  208.     current screen for subsequent operations if success.
  209.     Returns 0 iff success. */
  210.   {
  211.     if (VSinuse == 0)
  212.         return(-5); /* -5=no ports in use */
  213.     if (VSIwn == w)
  214.         return(0);    /* Currently set to that window */
  215.     if ((w > VSmax) || (w < 0))
  216.         return(-6); /* blown out the top of the stuff */
  217.     VSIwn = w;
  218.     if (VSscreens[w].stat != 1)
  219.         return(-3);/* not currently active */
  220.     VSIw = VSscreens[w].loc;
  221.     if (VSIw == 0L)
  222.         return(-3); /* no space allocated */
  223.     return(0);
  224.   } /* VSvalids */
  225.  
  226. VSscrn *VSwhereis(short i) /* screen number */
  227.   /* returns a pointer to the structure for the specified screen. */
  228.   {
  229.     VSvalids(i);
  230.     return(VSIw);
  231.   } /* VSwhereis */
  232.  
  233. void VSIclrbuf
  234.   (
  235.     void
  236.   )
  237.   /* clears out the text and attribute buffers for the current screen.
  238.     All text characters are set to blanks, and all attribute bytes
  239.     are set to zero. Doesn't update the display. */
  240.   {
  241.     register short j, i;
  242.     register char *tx;
  243.     register unsigned short *ta;
  244.     for (i = 0; i <= VSIw->lines; i++)
  245.       {
  246.         ta = &VSIw->attrst[i]->text[0];
  247.         tx = &VSIw->linest[i]->text[0];
  248.         for (j = 0; j <= VSIw->allwidth; j++)
  249.           {
  250.             *ta++ = 0;
  251.             *tx++ = ' ';
  252.           } /* for */
  253.       } /* for */
  254.   } /* VSIclrbuf */
  255.  
  256. short VSnewscreen
  257.   (
  258.     short maxlines, /* max lines to save in scrollback buffer */
  259.     short screensave, /* whether to have a scrollback buffer */
  260.     short numLines, //numLines initially on screen  (CCP 2.7)
  261.     short maxwid, /* number of columns on screen */
  262.     short forcesave    /* NCSA 2.5: force lines to be saved */
  263.   )
  264.   /* creates a new virtual screen, and returns its number. */
  265.   {
  266.  
  267.     if (maxlines < VSDEFLINES)
  268.         maxlines = VSDEFLINES;
  269.  
  270.     if (VSinuse >= VSmax)
  271.       /* too many screens in existence */
  272.         return(-1);
  273.     VSIwn = 0;
  274.     while ((VSIwn < VSmax) && (VSscreens[VSIwn].stat == 1))
  275.         VSIwn++;
  276.     if (VSIwn >= VSmax)
  277.       /* shouldn't occur? */
  278.         return(-1);
  279.     numLines -= 1;  //correct for internal use
  280.     
  281. /*
  282. *  Fill initial scrollback buffer and screen storage space.
  283. *
  284. *  Memory allocation rules:
  285. *  line->mem == 0 if not a memory allocation, line->mem == 1 if it is the first
  286. *     VSline in a block (indeterminate size, may be size == 1)
  287. *  
  288. *  attributes array is ALWAYS allocated as one block.  Internally represented and
  289. *  manipulated as a linked list of lines, but only one of the lines will have 
  290. *  line->mem == 1.  This list is always supposed to be circular (it is never
  291. *  extended, as attributes are never scrolled back).
  292. *
  293. *  scrollback and screen line buffer space is allocated in large blocks.  Each
  294. *  block will have line->mem == 1 if the pointer to that VSline is "free"able.
  295. *  This list will either be circular (which means it has reached its full size),
  296. *  or it will have a NULL next field at the end.  During scrolling, the end may
  297. *  be augmented until VSIw->numlines > VSIw->maxlines or we run out of memory.
  298. *  Typically allocate memory 100 lines at a time in two blocks, one is the VSline
  299. *  list, the other is the mem for the character storage.
  300. *
  301. */
  302.  
  303. /* All memory allocation for this function is done at once, to help damage control in 
  304. low memory situations */
  305.  
  306.     if ((VSscreens[VSIwn].loc = VSIw = (VSscrn *) myNewPtr(sizeof(VSscrn))) == 0L)
  307.         return(-2);
  308.  
  309.     VSIw->lines = numLines;
  310.     //VSIw->lines = 23; CCP 2.7 set this from the start
  311.  
  312.     VSIw->linest = VSInewlinearray(VSIw->lines + 1);
  313.     if (VSIw->linest == NULL)
  314.      {
  315.         DisposePtr((Ptr)VSIw);
  316.         VSscreens[VSIwn].loc = VSIw = NULL;
  317.         return (-2);
  318.     }    
  319.     
  320.     VSIw->attrst = VSInewattrlinearray(VSIw->lines + 1);
  321.     if (VSIw->attrst == NULL)
  322.      {
  323.         DisposePtr((Ptr)VSIw->linest);
  324.         DisposePtr((Ptr)VSIw);
  325.         VSscreens[VSIwn].loc = VSIw = NULL;
  326.         return (-2);
  327.     }    
  328.  
  329.     VSIw->tabs = (char *) myNewPtr(132);        /* NCSA: SB - allow 132 column mode */
  330.     if (VSIw->tabs == NULL)  /* CCP: Hey?  Why not check if we got it?! */
  331.     {
  332.         DisposePtr((Ptr)VSIw->attrst);
  333.         DisposePtr((Ptr)VSIw->linest);
  334.         DisposePtr((Ptr)VSIw);
  335.         VSscreens[VSIwn].loc = VSIw = NULL;
  336.         return (-2);
  337.     }
  338.  
  339.     VSIw->allwidth = 131;                /* NCSA: SB - always allocate max lines */
  340.  
  341.     if (screensave)
  342.         VSIw->buftop = VSInewlines(VSIw->lines + 1 + VSDEFLINES,1); /* screen lines plus some initial preallocated scrollback space */
  343.     else
  344.         VSIw->buftop = VSInewlines(VSIw->lines + 1,1); /* screen lines, no scrollback */
  345.     if (VSIw->buftop == NULL)
  346.     {
  347.         DisposePtr((Ptr)VSIw->tabs);
  348.         DisposePtr((Ptr)VSIw->attrst);
  349.         DisposePtr((Ptr)VSIw->linest);
  350.         DisposePtr((Ptr)VSIw);
  351.         VSscreens[VSIwn].loc = VSIw = NULL;
  352.         return(-2);
  353.     }
  354.     VSIw->linest[0] = VSIw->buftop;
  355.     VSIw->attrst[0] = (VSattrlinePtr)VSInewlines(VSIw->lines + 1,2);        /* new space for attributes (these are never scrolled back) */
  356.     if (VSIw->attrst[0] == NULL)
  357.     {
  358.         VSIfreelinelist(VSIw->buftop);
  359.         DisposePtr((Ptr)VSIw->tabs);
  360.         DisposePtr((Ptr)VSIw->attrst);
  361.         DisposePtr((Ptr)VSIw->linest);
  362.         DisposePtr((Ptr)VSIw);
  363.         VSscreens[VSIwn].loc = VSIw = NULL;
  364.         return(-2);
  365.     }
  366.  
  367.     VSIw->vistop = VSIw->scrntop = VSIw->buftop;    /* initial view = screen */
  368.  
  369.     VSIlistndx(VSIw->scrntop, VSIw->attrst[0]);    /* Set up screen arrays */
  370.     
  371.     VSIw->attrst[0]->prev = VSIw->attrst[VSIw->lines]; /* make attribute list circular, since it is never extended */
  372.     VSIw->attrst[VSIw->lines]->next = VSIw->attrst[0];
  373.     
  374.     if (!screensave)
  375.     {   /* make text line list circular to indi